<?php
    //购物车页面控制器
    class CartAction extends Action{
        //首页
        public function index(){
            
        }
        
        //加载购物车
        public function loadCart(){
            $userId=1;
            $cartItem=handleCartItem(getCartItems(1));
            $this->assign('cartList', $cartItem)->display('Cart');
        }
        
        //刷新保存购物车修改
        public function updateCart(){
            $arr= $_POST["array"];
            
            for($i=0; $i<count($arr)-1;$i++){
                $data['ItemId'] = $arr[$i][0];
                $data['ProCount'] = $arr[$i][1];
                $data['YnCheck'] = $arr[$i][2];
                $result=M('cart_item')->save($data);
                //p($result);
            }
        }
        
        //提交订单处理
        public function submitOrder(){
            $userId=$_POST["userId"];
            $orderId=time()*10+$userId;
            $address=M('user_info')->where("UserId='$userId' and YnDefault='ui-icon-checked-s'")->select();
            //p($address);
            $data['OrderId']=$orderId;
            
            if(!checkSubOrder($userId)){//检测库存
                $this->ajaxReturn(33);
            }
            
            if(count($address)>0){
                $data['UserName']=$address[0]['TakeName'];
                $data['UserPhone']=$address[0]['TakePhone'];   
                $data['Address']=$address[0]['TakeAddress'];
            }
            
            //订单
            $data['OrderState']=1;
            $data['CreateDate']=time();
            $data['UserId']=$userId;
            $data['Demand']=$_POST['demand'];
            $data['Total']=reckon($userId);
            
            
            //p($data);
            $result=M('order')->add($data);
            if(count($result)<=0){
                return 0;
            }
            $items=array();
            if($result!=0){
                //订单细目
                $temp= M('Cart_item')->where("UserId='$userId' and YnCheck='checked'")->select();
                $itemResult;//item状态
                foreach ($temp as $key => $value) {
                    $data2['OrderId']=$result;
                    $data2['ProId']=$value['ProId'];
                    $data2['ProCount']=$value['ProCount'];
                    $itemResult=M('order_item')->add($data2);
                    if(count($itemResult)==0){
                        $this->ajaxReturn(0);
                    }
                    array_push($items,(M('order_item')->where("ItemId='$itemResult'")->select()[0]['ProId']));
                }
            }
            
            //更新商品销售信息
            $os=getOrderItem($result);
            //p($os[0]['ProId']);
            foreach ($os as $key => $value) {
                updateProInfo($value['ProId'],$value['ProCount'],1);
            }
            
            
            
            array_push($items,$result);
            clearCart($userId);
            $this->ajaxReturn($items);
        }
        
        //从购物车中移除指定商品
        public function removePro(){
            $ItemId= $_POST["ItemId"];
            //echo $ItemId;
            $result=M('cart_item')->where("ItemId='$ItemId'")->delete();
            //echo ($result);
        }
        
        //添加商品到购物车
        public function addProToCart(){
            //读取参数
            $uId=$_POST['UserId'];
            $pId=$_POST['ProId'];
            
            $data['ProId']=$pId;
            $data['UserId']=$uId;
            $data['ProCount']=$_POST['ProCount'];
            $data['ProType']="默认";
            
            if(getProCount($_POST['ProId'])<$_POST['ProCount']){//检测库存
                $this->ajaxReturn(33);
            }

            $result;
            //保存
            $pC=M('cart_item')->where("UserId='$uId' and ProId='$pId'")->select();
            
            if(count($pC)>0){//检测购物车中是否已有此商品
                $data['ProCount']=intval($data['ProCount'])+intval($pC[0]['ProCount']);
                $data['ItemId']=$pC[0]['ItemId'];
                $result=M('cart_item')->save($data);
                //p($result);
            }else{
                $result=M('cart_item')->add($data);
            }
            
            
            //返回结果
            $this->ajaxReturn($result);
        }
        
    }
?>